﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class Message_AddMessage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtDept.Attributes.Add("onclick", "javascript:OpenTree(200,300)");
        if (!IsPostBack)
        {
            string msgId = Request.QueryString["mid"];
            if (msgId != null)
            {
                Message msg = new MessageManager().GetMessageById(Convert.ToInt32(msgId));
                this.txtTitle.Text = msg.Title;
                this.DropDownList1.Text = msg.Type.MessageTypeName;
                this.txtBeginTime.Text = msg.BeginTime.ToString();
                this.txtEndTime.Text = msg.EndTime.ToString();
                this.txtContent.Text = msg.Contents;
            }
            this.rbSelect.Checked = true;
            this.cbName.Checked = true;
            this.txtDept.Enabled = false;
            this.txtStfNo.Enabled = false;
            this.txtBeginTime.Text = DateTime.Now.ToShortDateString() + "  00:00:00";
            this.txtEndTime.Text = DateTime.Now.ToShortDateString() + "  23:59:59";
        }
       

    }
    protected void cbDept_CheckedChanged(object sender, EventArgs e)
    {
        if (this.txtDept.Enabled == false)
        {
            this.txtDept.Enabled = true;
        }

    }
    protected void cbStfNo_CheckedChanged(object sender, EventArgs e)
    {
        if (this.txtStfNo.Enabled == false)
        {
            this.txtStfNo.Enabled = true;
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Message msg = new Message();
        msg.BeginTime = Convert.ToDateTime(this.txtBeginTime.Text);
        msg.Contents = this.txtContent.Text.Trim();
        msg.EndTime = Convert.ToDateTime(this.txtEndTime.Text.Trim());
        msg.FromUserId = "admin";
        msg.IfPublish = 0;
        msg.RecordTime = Convert.ToDateTime(this.txtEndTime.Text);
        msg.Title = this.txtTitle.Text.Trim();
        MessageType type = new MessageType();
        type.MessageTypeId = Convert.ToInt32(this.DropDownList1.SelectedItem.Value);
        msg.Type = type;
        if (Request.QueryString["mid"]== null)
        {
            //添加Message,添加发送对象
            new MessageManager().AddMessage(msg);
            AddMsgToUser1();
        }
        else
        {
           
            //更新Messager,删除这条消息的发送对象，再添加新的对象

            msg.MessageId = Convert.ToInt32(Request.QueryString["mid"]);
            new MessageManager().UpdateMessage(msg);
            new MsgToUserManager().DeleteMessageToUserByMessageId(Convert.ToInt32(Request.QueryString["mid"]));
           AddMsgToUser();


        }
    }

    private void AddMsgToUser1()
    {
        List<Message> mlist = new MessageManager().GetAllMessage();
        int maxMessageId = 0;
        foreach (Message item in mlist)
        {
            if (maxMessageId < item.MessageId)
            {
                maxMessageId = item.MessageId;
            }
        }
        MessageToUser tu = new MessageToUser();
        tu.MessageId = maxMessageId;
        tu.IfRead = 0;
        if (this.rbAll.Checked == true)
        {
            tu.ToUserId = "所有人";
            new MsgToUserManager().AddMessageToUser(tu);
        }
        if (this.rbSelect.Checked == true)
        {
            foreach (ListItem item in cbList.Items)
            {
                tu.ToUserId = item.Text;
                new MsgToUserManager().AddMessageToUser(tu);
            }
        }
    }

    private void AddMsgToUser()
    {
        MessageToUser tu = new MessageToUser();
        tu.MessageId = Convert.ToInt32(Request.QueryString["mid"]);
        tu.IfRead = 0;
        if (this.rbAll.Checked == true)
        {

            tu.ToUserId = "所有人";

            new MsgToUserManager().AddMessageToUser(tu);
        }
        if (this.rbSelect.Checked == true)
        {
            foreach (ListItem item in cbList.Items)
            {
                tu.ToUserId = item.Text;
                new MsgToUserManager().AddMessageToUser(tu);
            }
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string dept = this.txtDept.Text.Trim();
        string no = this.txtStfNo.Text.Trim();
        string name = this.txtName.Text.Trim();
        List<Staff> list = new StaffManager().GetAllStaffByDeptCodeName(dept, no, name);
        if (list.Count != 0)
        {
            foreach (Staff item in list)
            {
                ListItem lll = new ListItem();
                lll.Selected = true;
                lll.Text = item.StfName;
                lll.Value = item.StfId.ToString();
                this.cbList.Items.Add(lll);
            }
        }

    }
}